package serverside;

import java.io.DataOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.JOptionPane;
import sun.misc.BASE64Encoder;

public class Screen {

    DataOutputStream outToClient;
    private Scanner input;
    private static final byte[] keyValue = new byte[]{'T', 'h', 'e', 'B', 'e',
        's', 't', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'};

    public Screen(DataOutputStream output) {
        outToClient = output;
        input = new Scanner(System.in);
    }

    public void sendDataToClient(String dataToSend) {
        try {
            outToClient.writeBytes(Encry(dataToSend) + '\n');
        } catch (IOException ex) {
            System.out.println("An error occured in Sending data to client ");
            System.out.print("If you want to view error send 1 or 0 otherwise");

            if (input.nextInt() == 1) {
                JOptionPane.showMessageDialog(null, "Error", ex.getMessage(),
                        JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception ex) {
            System.out.println("An error occured in Sending data to client <Annon one>");
            System.out.println("If you want to view error send 1 or 0 otherwise: ");

            if (input.nextInt() == 1) {
                ex.printStackTrace();
            }
        }
    }

    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, "AES");
        return key;
    }

    // Encryption technique .
    private static String Encry(String value) {
        try {
            Key key = generateKey();

            Cipher desCipher;

            // Create the cipher
            desCipher = Cipher.getInstance("AES");

            // Initialize the cipher for encryption
            desCipher.init(Cipher.ENCRYPT_MODE, key);

            //sensitive information
            byte[] text = desCipher.doFinal(value.getBytes());

            //System.out.println("Text [Byte Format] : " + text);
            //System.out.println("Text : " + new String(text));
            // Encrypt the text
            String Encrypted = new BASE64Encoder().encode(text);

            return Encrypted;

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
            return "";
        } catch (InvalidKeyException e) {
            e.printStackTrace();
            return "";
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
            return "";
        } catch (BadPaddingException e) {
            e.printStackTrace();
            return "";
        } catch (Exception ex) {
            Logger.getLogger(ATM.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;

    }
}//end class screen
